package com.then.component.login.ui.register;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.databinding.Observable;
import android.os.Bundle;
import android.view.View;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luojilab.router.facade.annotation.Autowired;
import com.luojilab.router.facade.annotation.RouteNode;
import com.then.component.login.R;
import com.then.component.login.ui.base.LoginModuleActivity;

import java.util.List;

/**
 * Created by 42524 on 2018/1/25.
 */

@RouteNode(path = "/registerCompletePage")
public class RegisterCompleteActivity extends LoginModuleActivity<RegisterCompleteDelegate> {

    private static final int REQUEST_CODE_CHOOSE = 1000;

    @Autowired
    String phoneNum;

    @Autowired
    String countryCode;

    @Autowired
    String pwd;

    @Autowired
    String authCode;

    RegisterCompleteActivityViewModel viewModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = ViewModelProviders.of(this, getViewModelFactory()).get(RegisterCompleteActivityViewModel.class);
    }

    @Override
    public void actionViewModel(View view, Object o, int actionType) {
        int id = view.getId();
        if (id == R.id.iv_user_avatar) {
            selectImg();
        } else if (id == R.id.tv_submit) {
            submit();
        }
    }

    private void selectImg() {
        PictureSelector.create(RegisterCompleteActivity.this)
                .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .theme(R.style.picture_default_style)//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
                .maxSelectNum(1)     // 最大图片选择数量 int
                .previewImage(true)// 是否可预览图片 true or false
                .isCamera(true)// 是否显示拍照按钮 true or false
                .imageFormat(PictureMimeType.PNG)// 拍照保存图片格式后缀,默认jpeg
                .sizeMultiplier(0.6f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                .enableCrop(true)// 是否裁剪 true or false
                .compress(true)
                .forResult(REQUEST_CODE_CHOOSE);//结果回调onActivityResult code
    }


    private void submit() {
        String nickName=viewDelegate.getNickName();
        String personSign=viewDelegate.getPersonSign();
        viewModel.checkNickName(nickName).observe(this,resource -> {
                    if(resource.data!=null){
                        viewModel.registerComplete(countryCode,phoneNum,authCode,pwd,nickName,pwd,"1").observe(this,resource1 -> {

                        });
                    }
                }
        );
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_CHOOSE) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            if (selectList == null || selectList.isEmpty()) {
                return;
            } else {
                String imgPath = selectList.get(0).getCompressPath();
                viewDelegate.setAvatarPath(imgPath);
                viewDelegate.setCircle(true);
                viewDelegate.setLocal(true);
            }
        }
    }


    @Override
    protected Class<RegisterCompleteDelegate> getDelegateClass() {
        return RegisterCompleteDelegate.class;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.setOnClickListener(this, R.id.tv_submit, R.id.iv_user_avatar);
    }
}
